﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Collections;
using WordList;
using MySql.Data.MySqlClient;

namespace DotnetWork
{
    public class MatchWordList
    {
        public string s = "";
        public HashSet<Word> markString(List<Word> sl)//sl里面应该没有重复的单词   hash?
        {//abandon         oiad [abandon jaowidj
            s=s.Replace("  ", "");
            HashSet<Word> matched = new HashSet<Word>();
            int cnt = 0;
            Hashtable ht = new Hashtable();
            string[] stringArray = Regex.Split(s, "\\s+", RegexOptions.IgnoreCase);
            for (int i = 0; i < sl.Count; i++)
            {
                if (!ht.ContainsKey(sl[i].Text))
                    ht.Add(sl[i].Text, "");
            }
            for (int i = 0; i < stringArray.Length; i++)
            {
                if (ht.ContainsKey(stringArray[i]))
                {
                    for (int j = 0; j < sl.Count; j++)
                    {
                        if (sl[j].Text == stringArray[i])
                        {
                            matched.Add(sl[j]);
                            break;
                        }
                    }
                    s = s.Insert(cnt, "[");
                    s = s.Insert(cnt + stringArray[i].Length + 1, "]");
                    cnt += stringArray[i].Length; cnt += 3;
                }
                else cnt += (stringArray[i].Length + 1);
            }
            return matched;
        }
        public static List<Word> CreateWordList()
        {
            using (MySqlConnection conn = UserService.GetConnection())
            {
                string sql = "select * from cet";
                using (MySqlCommand command = new MySqlCommand(sql, conn))
                {
                    using (MySqlDataReader reader = command.ExecuteReader())
                    {
                        List<Word> words = new List<Word>();
                        while (reader.Read())
                        {
                            if (reader.HasRows)
                            {
                                words.Add(new Word(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), 0, 0));
                            }
                        }
                        return words;
                    }
                }
            }
        }
    }
}
